package com.thkj.controller;

import com.thkj.entity.TblSecurityAdmin;
import com.thkj.service.TblSecurityAdminService;
import com.thkj.util.RequestUtil;
import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import dp.util.UUIDUtil;
import com.thkj.framework.base.BaseController;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 监管员Action
 * @author weihuu
 *
 */
@RestController
@RequestMapping("/backstage/sysuser")
public class SysuserAction extends BaseController {
	
    @Autowired
    TblSecurityAdminService tblSecurityAdminService;
    
	@RequestMapping("")
    public ModelAndView index(){
    	return new ModelAndView("sysuser/list");
    }
	
	@RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size){
    	Map<String, Object> param = RequestUtil.getParameterMap(request);
    	PagingList<Map> list = tblSecurityAdminService.query(page, size,param);
    	return R.success(list);
    }
	
    @RequestMapping("/edit")
    public R edit(TblSecurityAdmin person){
    	try {
    		if(StringUtils.isBlank(person.getId())) {
    			person.setId(UUIDUtil.randomUUID());
				person.setPassword(DigestUtils.md5Hex(person.getPassword()));
    			person.setCreateDate(DateUtil.getNowAsTimestamp());
    			tblSecurityAdminService.insertTblSecurityAdmin(person);
        	}else {
        		tblSecurityAdminService.updateTblSecurityAdmin(person);
        	}
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/setAcount")
    public R setAcount(TblSecurityAdmin person){
    	try {
    		person.setPassword(DigestUtils.md5Hex(person.getPassword()));
        	tblSecurityAdminService.setAcount(person);
        	return R.SUCCESS;
    	}catch (Exception e) {
			return R.failure(e.getMessage());
		}
    }
    
    @RequestMapping("/delete")
    public R delete(String id){
    	tblSecurityAdminService.deleteTblSecurityAdmin(id);
    	return R.SUCCESS;
    }
}